package com.googlecode.uipipedream.impl;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.Rectangle;

import com.googlecode.uipipedream.spec.ColourModel;
import com.googlecode.uipipedream.spec.HSBType;

/**
 * <p>
 * This class is heavily influenced by Horizontal Slider Bar. It's inhereted to
 * break up the logic between two classes. This might be better to flag using
 * HSBColour and have processed internally by the methods they over-ride.
 * </p>
 * 
 * <p>
 * This file is part of UIPipeDream.
 * </p>
 * 
 * <p>
 * You can redistribute UIPipeDream and/or modify it under the terms of the GNU
 * General Public License as published by the Free Software Foundation, either
 * version 3 of the License, or (at your option) any later version.
 * </p>
 * 
 * <p>
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * </p>
 * 
 * <p>
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <a
 * href="http://www.gnu.org/licenses/">http://www.gnu.org/licenses/</a>.
 * </p>
 * 
 * @author <a href="http://daemonsadvocate.blogspot.com/">Matthew B. Gray</a>
 * @author James Jones
 * @version 1.0 Final
 */
public class ComplementaryHorizontalSliderBar extends HorizontalSliderBar{
	// TODO: Refactor so that methods here are switched using HSBColour in superclass.
	private static final long serialVersionUID = 7412447324173839051L;

	public ComplementaryHorizontalSliderBar(ColourModel model, HSBType type) {
		super(model, type);
	}
	
	
	@Override
	protected float getHue(int x) {
		final float toReturn = ((float)x / super.rainbowBar.width) % 1f;
		return toReturn;
	}
	
	@Override
	protected void paintColourSelectors() {
		final float[] hues = model.getSplitComplementHues();
		Color currColour;
		for (int i=0; i<nSelectors.length; i++) {
			currColour = Color.getHSBColor(hues[i], model.getSaturation(), model.getBrightness());
			
			graphics.setColor(currColour);
			graphics.fillRect(
					nSelectors[i].x, nSelectors[i].y,
					nSelectors[i].width, nSelectors[i].height);
			
			graphics.setColor(Color.white);
			graphics.drawRect(
					nSelectors[i].x, nSelectors[i].y,
					nSelectors[i].width, nSelectors[i].height);
		}
	}
	
	@Override
	protected void refreshDrawableItems(Dimension d) {
		super.nSelectors = new Rectangle[super.model.getNColours()-1];
		Point leftCorner, rightCorner;
		float incrementalOffset;
		final int centerOfBar = rainbowBar.x + rainbowBar.width/2;
		final int halfNSelectors = super.nSelectors.length/2;
		for (int i=halfNSelectors; 0<i; i--) {
			// 1.1. Setup incremental offset as a size
			incrementalOffset = i * model.getOffset() * rainbowBar.width;
			
			// 1.2. Create corner points from center value.
			leftCorner = new Point(
					(int)(centerOfBar - incrementalOffset - super.stdOutrigger.width/2),
					(int)(0.02 * d.height));
			rightCorner = new Point(
					(int)(centerOfBar + incrementalOffset - super.stdOutrigger.width/2),
					(int)(0.02 * d.height));
			
			// 1.3. Assign to selectors
			super.nSelectors[halfNSelectors - i] = new Rectangle(leftCorner, super.stdOutrigger);
			super.nSelectors[halfNSelectors + i - 1] = new Rectangle(rightCorner, super.stdOutrigger);
		}
	}
}
